<?php

class Gallery_Backend_Photos_Add extends Gallery_Backend_Photos_Edit 
{
    
    const FILES_NUM = 5;
    
    /**
     * Initialize form elements. 
     *
     */
    protected function _initElements() 
    {
        parent::_initElements();
        
        
        /*$i = 2;
        while($i <= self::FILES_NUM) {
            
            $this->_addElement('file_' . $i, 'file', array(
                'type' => 'file', 
                'save' => array($this, 'file_' . $i), 
                'validators' => array(
                    array('image', array(
                        //'types' => Kernel_Config::get('gallery', 'photo_types'), 
                        'message_unsupported_type' => 'PHOTO_UNSUPPORTED_TYPE',
                    ))
                )));
            
            $i++;
        }*/
            
    }
    
    public function __call($method, $args) 
    {
        if (false !== strpos($method, 'file_')) {
            $this->file(array_shift($args));
        }
    }
    
    /**
     * Add new record.
     *
     */
    protected function _insert() 
    {
        // required file
        $this->id = database::insert($this->_table, $this->_data());
        
        
        // the rest optional
        $files = array_filter
        (
            array_extract($this->_getValues(), 
                array_map
                (
                    create_function('$value', 'return "file_" . $value;'), 
                    range(2, 5))
                ), 
            
            create_function('$file', 'return strlen($file->__toString());')
        );
        foreach ($files as $file) {
            database::insert($this->_table, array(
                'album_id' => $this->_getValue('album_id'), 
                'file' => $file->__toString(), 
            ));
        }
    }
    
}
